<?php
function smarty_function_interests_inputs($params, &$smarty) {
	
	$sql = "SELECT i.id, i.name, ivp.portfolio_id, ivp.disposition FROM silk_interests AS i
			LEFT OUTER JOIN `silk_interests_vs_portfolios` AS ivp ON i.id = ivp.interest_id
			AND ivp.portfolio_id = $params[portfolio_id]";
	
	$results = db()->GetArray($sql);
	
	$all_interests = array();
	$interests = array();
	$disinterests = array();
	foreach( $results as $result ) {
		$all_interests[$result["id"]] = $result["name"];
		// put these in backwards so we can use in_array further on
		if( $result["disposition"] == "Y" ) {
//			$interests[$result["id"]] = $result["name"];
			$interests[$result["name"]] = $result["id"];
		}
		if( $result["disposition"] == "N" ) {
			$disinterests[$result["name"]] = $result["id"];
		}
	}
	
//	echo "<pre>"; var_dump($interests);var_dump($disinterests);var_dump($all_interests); echo "</pre>";
	$output = "<table>";
	foreach( $all_interests as $interest_id => $interest_name ) {
		$output .= "<tr><td>$interest_name</td><td>";
		if( in_array( $interest_id, $interests )) {
			$output .= "<input type='radio' name='interest_{$interest_id}' value='Y' checked>Interested<br />";
		} else {
			$output .= "<input type='radio' name='interest_{$interest_id}' value='Y'>Interested<br />";
		}
		
		if( in_array( $interest_id, $disinterests )) {
			$output .= "<input type='radio' name='interest_{$interest_id}' value='N' checked>Not Interested<br />";
		} else {
			$output .= "<input type='radio' name='interest_{$interest_id}' value='N'>Not Interested<br />";
		}
		
		if( !in_array( $interest_id, array_merge( $interests, $disinterests ))) {
			$output .= "<input type='radio' name='interest_{$interest_id}' value='D' checked>No preference<br />";
		} else {
			$output .= "<input type='radio' name='interest_{$interest_id}' value='D'>No preference<br />";
		}
		$output .= "</td></tr>";
	}
	$output .= "</table>";
	return $output;
	
//	echo "<pre>"; var_dump($interests);var_dump($disinterests);var_dump($all_interests); echo "</pre>";
}
?>